//
// Created by Toosan on 2022/4/23.
//

#include "iostream"

using namespace std;

class Layout {
private :
    int x, y;
public:
    Layout() {};

    Layout(int x, int y) : x(x), y(y) {};

    int getX() const {
        return this->x;
    }

    void setX(int x) {
        this->x = x;
    }

    int getY() const {
        return this->y;
    }

    void setY(int y) {
        this->y = y;
    }

    void showLayout() const {
        cout << "x = " << this->x << ", y = " << this->y << endl;
    }

    /**
     * 类中实现操作符重载
     * @param layout1
     * @return
     */
//    Layout operator+(Layout layout1) const {
//        return {this->x + layout1.getX(), this->y + layout1.getY()};
//    }

    /**
     * 系统中的实现写法(只读)  layout1前面为什么要加 “&”
     * 提高性能，&地址传递，加了之后，调用重载符就不会创建新的地址了
     * @param layout1
     * @return
     */
    Layout operator+(const Layout &layout1) const {
        /**
         * 为什么不使用layout1.getX()，这里我已经在getX()加了const,所以可以调用
         * 如果getX()没有加const修饰，调用layout1.getX()是会报错的
         * 因为它怕你在getX()函数里面对x的值进行了修改，所以只能让你调用layout1.x
         */
        return {this->x + layout1.x, this->y + layout1.y};
    }

    /**
     * ++对象
     * @return
     */
    Layout operator++() const {
        return {this->x + 1, this->y + 1};
    }

    /**
     * 对象++
     * @return
     */
    Layout operator++(int) {
        Layout layout(this->x, this->y);
        this->x = this->x + 1;
        this->y = this->y + 1;
        return layout;
    }

//    friend void operator<<(ostream &_START, Layout &layout) {
//        _START << "layout:" << layout.x << ", " << layout.y << endl;
//    }

    // 链式调用
    friend ostream &operator<<(ostream &START, Layout &layout) {
        START << "layout:" << layout.x << ", " << layout.y << endl;
        return START;
    }

    friend istream &operator>>(istream &START, Layout &layout) {
        START >> layout.x;
        START >> layout.y;
//        START >> layout.x >> layout.y;
        return START;
    }
};

/**
 * 实现操作符重载，一般在类里面实现
 * @param layout1
 * @param layout2
 * @return Layout
 */
//Layout operator+(Layout layout1, Layout layout2) {
//    int x = layout2.getX() + layout1.getX();
//    int y = layout2.getY() + layout1.getY();
//    return {x, y};
//}

class ArrayList {
private:
    int size = 0; // 使用的时候要先初始化，给个默认值，不然默认是个系统值，无意义的。
    int arrayValue[10];
//    int *arrayValue;


public:
    void setValue(int index, int value) {
        arrayValue[index] = value;
        size++;
    }

    int getSize() const {
        return size;
    }

    int operator[](int index) {
        return this->arrayValue[index];
    }

    void printToString() {
        for (int i = 0; i < size; ++i) {
            cout << "index = " << i << ", value = " << arrayValue[i] << endl;
        }
    }
};

//void printArray(ArrayList arrayList) {
//    for (int i = 0; i < arrayList.getSize(); ++i) {
//        cout << "index = " << i << ", value = " << arrayList[i] << endl;
//    }
//}

/**
 * 操作符重载、运算符重载
 * 对象1 + 对象2 C++默认是不支持的，Java、Kotlin都不支持
 * 可以通过操作符重载 把 “+” 进行重载
 * @return
 */
int main_day11_1() {
//int main() {

    Layout layout1(100, 200);
    Layout layout2(300, 400);
    // 对 “+” 进行操作符重载
    Layout layout = layout1 + layout2;
    layout.showLayout();
    layout = ++layout;
    layout.showLayout();
    layout++;
    layout.showLayout();

//    cout << layout;
    cout << layout << layout << layout;

//    Layout layout3;
//    cin >> layout3;
//    cout << layout3;

    ArrayList arrayList;
    arrayList.setValue(0, 1);
    arrayList.setValue(1, 11);
    arrayList.setValue(2, 111);
    cout << arrayList.getSize() << endl;
//    printArray(arrayList);
    arrayList.printToString();
    return 0;
}